<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>地址管理</title>
    <link rel="stylesheet" type="text/css" href="../../../css/aui.css" />
    <style>
        .text-light {
            color: #999 !important;
        }

        .aui-radio {
            width: 1rem;
            height: 1rem;
        }

        .aui-card-list {
            border: 0.1rem solid #fff;
            box-sizing: border-box;
        }
        .aui-card-list.active {
            border: 0.1rem solid #f00;
            box-sizing: border-box;
        }
    </style>
</head>

<body>
    <section class="aui-content" id="app">
        <div class="aui-card-list" v-for="o,i in list">
            <div class="aui-card-list-header aui-font-size-14" @click='activeAddr(o,i)'>
                <div>{{o.contact}}</div>
                <div>{{o.contact_phone}}</div>
            </div>
            <div class="aui-card-list-content-padded aui-padded-t-0 text-light" @click='activeAddr(o,i)'>
                {{o.region}} {{o.address}}
            </div>
            <div class="aui-card-list-footer aui-border-t">
                <div><span v-if="o.is_default">默认地址</span></div>
                <div>
                    <label @click="updateAddr(o.address_id,i)">
                        <i class="aui-iconfont aui-icon-edit aui-margin-r-5 aui-font-size-14"></i>编辑
                    </label>
                    <label @click="removeAddr(o.address_id,i)">
                        <i class="aui-iconfont aui-icon-trash aui-margin-r-5 aui-margin-l-15 aui-font-size-14"></i>删除
                    </label>
                </div>
            </div>
        </div>

    </section>
</body>
<script src="../../../script/api.js" charset="utf-8"></script>
<script src="../../../script/aui/aui-tab.js" charset="utf-8"></script>
<script src="../../../script/common/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="../../../script/common/common.js" charset="utf-8"></script>
<script src="../../../script/common/config.js" charset="utf-8"></script>
<script src="../../../script/common/vue.min.js" charset="utf-8"></script>
<script type="text/javascript">
    apiready = function() {
        api.parseTapmode();
        vm.init();

    }

    var vm = new Vue({
        el: "#app",
        data: {
            userInfo: {},
            list: [],

            listType: 1,
            listPage: 1,
            listStatus: true,

            selectAddr: ''
        },
        methods: {
            init() {
                this.upDownLoad();
                this.userInfo = get_local("userInfo");
                this.getList();
                this.eventListener();


            },
            getList() {
                var that = this;
                var data = {
                    user_id: this.userInfo.user_id,
                    pages: this.listPage
                };
                _ajax(Config.api.UserAddressGetLists, data, function(ret) {
                    if (ret.code === 200) {
                        if (ret.data.length > 0) {
                            that.list = that.list.concat(ret.data);
                        } else {
                            that.listStatus = false;
                        }
                    }
                });
            },
            updateAddr(id) {
                var name = "add_addr_win";
                var url = "./add_addr_win.html";
                open_w(name, url, {
                    id: id,
                    type: 'update'
                });
            },
            removeAddr(id, i) {
                var that = this;
                var data = {
                    user_id: this.userInfo.user_id,
                    address_id: id
                };
                _ajax(Config.api.UserAddressRemove, data, function(ret) {
                    if (ret.code === 200) {
                        that.list.splice(i, 1);
                        $toast("删除成功");
                    }
                });
            },
            activeAddr(o,i) {
                var that = this;
                if (api.pageParam.type === 'select') {
                    that.selectAddr = o;
                    $(".aui-card-list").eq(i).addClass("active").siblings().removeClass('active');
                }
            },
            confirmAddr() {
                if (this.selectAddr === '') {
                    $toast('请选择地址');
                } else {
                    // 发送监听
                    api.sendEvent({
                        name: 'selectAddr',
                        extra: this.selectAddr
                    });
                    close_w();
                }
            },
            eventListener() {
                var that = this;
                api.addEventListener({
                    name: 'updateAddr',
                }, function(ret, err) {
                    that.listStatus = true;
                    that.listPage = 1;
                    that.list = [];
                    that.getList();
                });
            },
            upDownLoad() {
                var that = this;

                // 下拉刷新
                push_down(function() {
                    //下拉刷新恢复
                    push_down_over();
                    that.listStatus = true;
                    that.listPage = 1;
                    that.list = [];
                    that.getList();
                });

                //上拉加载
                push_up(function() {
                    if (that.listStatus) {
                        that.listPage++;
                        that.getList();
                    }
                });
            },
        }
    });
</script>

</html>
